package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService; //菜品
    @Autowired
    SetmealService setmealService;//套餐
    @Override
    public void deleteById(Long id) {
    //查询是否关了菜品
        Integer count1 = dishService.query().eq("category_id", id).count();
        if (count1>0) {
            throw new CustomerException("此分类关联了菜品 无法删除");
        }
        //查询是否关联套餐
        Integer count2 = setmealService.query().eq("category_id", id).count();
        if (count2>0) {
            throw new CustomerException("此分类关联了套餐 无法删除");
        }
        //代码能执行到这说明没有关联其他东西 可以删除
        this.removeById(id);
    }
}
